package com.senyint.imsdk;

import com.senyint.imsdk.util.StringUtil;

public class Configuration implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3796992355235828656L;

	private static String APP_KEY = "senyint-appkey" ;
	
	public final static int ACTION_DONE = 1;
	public final static int ACTION_NONE = 0;
	
	public static final String XMPP_HOST = "host";
	public static final String XMPP_PORT = "port";
	public static final String XMPP_SERVICE_NAME = "service";
	public static final String XMPP_RESOURCE_NAME = "resource";
	public static final String XMPP_USER_NAME = "user";
	public static final String USER_TOKEN = "usr_token";
	
	public static final Integer FILE_TYPE_IMAGE = 2;
	public static final Integer FILE_TYPE_VOICE = 3;
	public static final Integer FILE_TYPE_UNKOWN = 99;
	
//	public static final String APP_UPLOAD_URI = "http://172.16.200.178:8080/openfire-web/app/file/push" ;
//	public static final String APP_DOWNLD_URI = "http://172.16.200.178:8080/openfire-web/app/file/down" ;
	public static final String APP_UPLOAD_URI = "http://imup.cinyi.com/fdfs/app" ;
	public static final String APP_DOWNLD_URI = "http://imdown.cinyi.com/" ;
	
	/**
	 * fetch the user's offlinemessage
	 */
	public static final String APP_OFFLINE_MESSAGE_URI = "http://message.cinyi.com/imuser/offline/" ;
	public static final String APP_HISTORY_MESSAGE_URI = "http://testimapi.cinyi.com/message/history" ;
	
	//192.168.20.140,10.129.183.186
	protected final static  String DEFAULT_HOST = "testim.cinyi.com" ;//"172.16.200.178","192.168.20.147";//////"192.168.20.140" ;////"192.168.20.140" ;// "192.168.20.147";//"10.129.184.195" ;
	protected final static  int 	DEFAULT_PORT = 5222 ; 
	protected final static  String DEFAULT_RESOURCE_NAME = "smack" ; 
	protected static final String XMPP_PASSWORD = "security";
	
	/**
	 * 通知用户登录冲突
	 */
	public static final String XMPP_USER_CONFLICT = "com.senyint.ACTION.conflict";
	/**
	 * 通知用户重新登陆成功
	 */
	public static final String XMPP_USER_RECONNECT_SUCCESS = "com.senyint.ACTION.reconnect.success";
	public static final String XMPP_CONN_FAILED = "com.senyint.ACTION.reconnect.failed";
	public static final String XMPP_CONN_RECONNING = "com.senyint.ACTION.reconnect.ing";
	public static final String XMPP_CONN_ERROR = "com.senyint.ACTION.reconnect.error";
	public static final String XMPP_CONN_CLOSED  = "com.senyint.ACTION.reconnect.closed";
	public static final String XMPP_AUTH_SUCCESS  = "com.senyint.ACTION.auth.success";
	public static final String XMPP_AUTH_FAILED  = "com.senyint.ACTION.auth.failed";
	
	public static final String EVENT_REQ_ALARM =  "com.android.dtp.ACTION.req.ALARM" ;      //check user's connection
    public static final String EVENT_REQ_LOGIN =  "com.android.dtp.ACTION.req.LOGIN" ;      //登入
    public static final String EVENT_REQ_LOGOUT =  "com.android.dtp.ACTION.req.LOGOUT" ;    //登出

    /**
     * login error for handler
     */
    public static final int MESSAGE_LOGIN_FAILED =  -1 ;
    /**
     * login successful for handler
     */
    public static final int MESSAGE_LOGIN_SUCCESS =  1 ;
    public static final int MESSAGE_LOGIN_AGAIN =  99 ;
    
	/**
	 *
	 */
	public final static Integer LOGIN_SUCCESS = 1;
	public final static Integer LOGIN_CONNECTION_DISCONNECT = -40 ;
	public final static Integer LOGIN_USER_ALREADY_LOGIN = -99 ;
	public final static Integer LOGIN_FAIL = -1;

	public static final int TYPE_MESSAGE = 0;
	public static final int TYPE_IQ = 1;

	private SharedPreferencesHelper prefHelper = null ;
	
	public Configuration() {
		System.out.println(System.getProperty("user.dir")+"\\smack-cfg.properteis");
		prefHelper = new SharedPreferencesHelper(System.getProperty("user.dir")+"\\smack-cfg.properteis");
	}
	
	public Configuration(String name) {
		prefHelper = new SharedPreferencesHelper(name);
	}

	public void remove(String key){
		prefHelper.removeAttribute(key);
	}
	
	public void set(String key,Object val){
		if(val == null) return ;
		else if(val instanceof Boolean)
			prefHelper.setAttribute(key, (Boolean)val);
		else if(val instanceof Integer)
			prefHelper.setAttribute(key, (Integer)val);
		else if(val instanceof Long)
			prefHelper.setAttribute(key, (Long)val);
		else 
			prefHelper.setAttribute(key,val.toString());
	}
	
	@SuppressWarnings("unchecked")
	public <T> T get(String key,T val){
		if(val instanceof Boolean)
			return (T) Boolean.valueOf(prefHelper.getAttribute(key));
		else if(val instanceof Integer)
			return (T) prefHelper.getAttribute(key, (Integer)val);
		else if(val instanceof Long)
			return (T) prefHelper.getAttribute(key, (Long)val);
		
		return (T) (prefHelper.getString(key)==null?val:prefHelper.getString(key));
		
	}
	
	public String getServiceName() {
		return prefHelper.getString(Configuration.XMPP_SERVICE_NAME, null) ;
	}

	public String getHost() {
		return prefHelper.getString(Configuration.XMPP_HOST,Configuration.DEFAULT_HOST) ;
	}

	public void setHost(String host) {
		prefHelper.setAttribute(Configuration.XMPP_HOST,host) ;
	}

	
	public String getResourceName() {
		return prefHelper.getString(Configuration.XMPP_RESOURCE_NAME,Configuration.DEFAULT_RESOURCE_NAME) ;
	}
	
	public String getUserName() {
		return prefHelper.getString(Configuration.XMPP_USER_NAME,"") ;
	}
	
	public String getPassword() {
		String pwd = prefHelper.getString(Configuration.XMPP_PASSWORD,"");
		if(!StringUtil.isEmpty(pwd)){
//			return MD5Util.decode(pwd);
			return new String(StringUtil.hexStringToByte(pwd));
		}
		return null ;
	}
	
	public void setPassword(String password) {
		set(XMPP_PASSWORD, StringUtil.bytesToHexString(password.getBytes()));
	}
	
	protected void setPort(int port){
		set(XMPP_PORT, port);
	}
	
	protected void setServiceName(String service){
		set(XMPP_SERVICE_NAME, service);
	}
	
	protected void setResourceName(String res){
		set(XMPP_RESOURCE_NAME, res);
	}

	public void setUserName(String login) {
		set(XMPP_USER_NAME, login);
	}

	public void removeUserName() {
		remove(XMPP_USER_NAME);
	}

	public void removePassword() {
		remove(XMPP_PASSWORD);
	}
	
	public void removeUserToken() {
		remove(USER_TOKEN);
	}
	
	public void removeResource() {
		remove(XMPP_RESOURCE_NAME);
	}

	public Object putString(String key, String valueOf) {
		// TODO Auto-generated method stub
		return null;
	}

}

